var viewData = {};
var show = {};

$('.func-erp li').click(function () {
    $('.func-erp li').removeClass('funcLI');
    $(this).addClass('funcLI');
})

$('#qiye').click(function () {
    show.qiye = !show.qiye;
    show.qiyem = !show.qiyem;
    if (show.qiye && !viewData.qiye && show.qiyem && !viewData.qiyem) {
        viewData.qiye = sgworld.Creator.AddGeoJSONFile('企业点', '../geojson/qiye.geojson', {
            projection: 'EPSG:4326',
            image: '../static/IMG/mark1.png',
            imageScale: 0.5,
            minZoom: 11
        });
        viewData.qiyem = sgworld.Creator.AddGeoJSONFile('企业面', '../geojson/qiyem.geojson', {
            projection: 'EPSG:4326',
            lineColor: 'rgba(255, 255, 255, 1)',
            fillColor: 'rgba(255, 255, 255, 0.2)',
            minZoom: 10
        });

        setTimeout(() => {
            let source = viewData.qiyem.getSource();
            let i = 1;
            source.forEachFeature(item => {
                item.values_.id = 'qiye' + i++;
            });
        }, 100);

        !show.isPick && _pickQiYeData();
        return;
    }
    viewData.qiye.setVisible(show.qiye)
    viewData.qiyem.setVisible(show.qiyem)
});

$('#pick').click(function () {
    _pickQiYeData();
});

//拾取
function _pickQiYeData() {
    if (!show.isPick) {
        !viewData.tooltip && (viewData.tooltip = sgworld.Core.createHelpTooltip(map));
        viewData.pickHandler = map.on('pointermove', mouseMoveEven);

        !viewData.resultTooltip && (viewData.resultTooltip = sgworld.Core.createResultTooltip(map, true));

        if (!viewData.selectHandler) {
            viewData.selectHandler = new ol.interaction.Select();
            viewData.selectHandler.setActive(true);
            map.addInteraction(viewData.selectHandler);

            let selected = viewData.selectHandler.getFeatures();
            selected.on('add', function (evt) {
                let pickData = evt.element.values_;
                if (!pickData || !pickData.id || (pickData.id && pickData.id.indexOf('qiye') === -1)) {
                    if (viewData.thisShowWindow && viewData.thisShowWindow.thisTap) {
                        let thisTap = viewData.thisShowWindow.thisTap;
                        if (thisTap === 'danger_source') {
                            viewData.thisShowWindow.dangerData && viewData.selectHandler.removeSelectedStyle_(viewData.thisShowWindow.dangerData);
                            viewData.thisShowWindow.dangerData = evt.element;
                            let Coordinates = viewData.thisShowWindow.dangerData.getGeometry().getInteriorPoints().getCoordinates();
                            Coordinates = [Coordinates[0][0], Coordinates[0][1]];
                            viewData.resultTooltip.showAt(pickData.name + '/' + pickData.qiye + '/' + pickData.level + '重大危险源', Coordinates);
                        } else if (thisTap === 'crafts') {
                            viewData.resultTooltip.show(false);
                            viewData.thisShowWindow.craftsData && viewData.thisShowWindow.craftsData.setStyle(new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/IMG/point1.png',
                                    scale: 0.5
                                })
                            }));
                            viewData.thisShowWindow.craftsData = evt.element;
                            viewData.thisShowWindow.clickData = evt.element;

                            viewData.thisShowWindow.craftsData.setStyle(new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/IMG/point1.png',
                                    scale: 0.8
                                })
                            }));
                            viewData.thisShowWindow.craftsData && showChartData(viewData.thisShowWindow.craftsData, pickData.qiye + '/' + pickData.name + '/' + pickData.gy);
                        } else if (thisTap === 'danger_goods') {
                            viewData.resultTooltip.show(false);
                            viewData.thisShowWindow.DangerGoodsData && viewData.thisShowWindow.DangerGoodsData.setStyle(new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/IMG/point.png',
                                    scale: 0.5
                                })
                            }));
                            viewData.thisShowWindow.DangerGoodsData = evt.element;
                            viewData.thisShowWindow.clickData = evt.element;

                            viewData.thisShowWindow.DangerGoodsData.setStyle(new ol.style.Style({
                                image: new ol.style.Icon({
                                    src: '../static/IMG/point.png',
                                    scale: 0.8
                                })
                            }));
                            viewData.thisShowWindow.DangerGoodsData && showChartData(viewData.thisShowWindow.DangerGoodsData, pickData.qiye + '/' + pickData.name + '/' + pickData.whp + '/' + pickData.num);
                        } else if (thisTap === 'danger_area') {
                            viewData.thisShowWindow.DangerAreaData && viewData.selectHandler.removeSelectedStyle_(viewData.thisShowWindow.DangerAreaData);
                            viewData.thisShowWindow.DangerAreaData = evt.element;

                            let Coordinates = viewData.thisShowWindow.DangerAreaData.getGeometry().getInteriorPoints().getCoordinates();
                            Coordinates = [Coordinates[0][0], Coordinates[0][1]];
                            viewData.resultTooltip.showAt(pickData.qiye + '/' + pickData.name, Coordinates);
                        }
                    } else if (pickData.wzxq) {
                        popupRes('物资详情', 550, 600, 'HTML/wzxq.html', {
                            cancel: () => {
                                viewData.selectHandler.getFeatures().clear();
                            }
                        });
                    } else {
                        viewData.selectHandler.getFeatures().clear();
                    }
                    return;
                }
                viewData.tooltip && viewData.tooltip.show(false);
                viewData.thisSelect && viewData.selectHandler.removeSelectedStyle_(viewData.thisSelect);
                viewData.thisSelect = evt.element;
                viewData.selectHandler.getFeatures().clear();
                viewData.selectHandler.giveSelectedStyle_(viewData.thisSelect);

                let Coordinates = viewData.thisSelect.getGeometry().getInteriorPoints().getCoordinates();
                Coordinates = [Coordinates[0][0], Coordinates[0][1]];
                viewData.resultTooltip.showAt(pickData["TE_DESC"] || pickData["name"], Coordinates);
                viewData.pickData = pickData;

                popupRes('基本信息', 500, 800, 'HTML/biote.html', {
                    cancel: () => {
                        viewData.resultTooltip.show(false);
                        viewData.selectHandler.getFeatures().clear();
                        viewData.thisSelect && viewData.selectHandler.removeSelectedStyle_(viewData.thisSelect);
                        viewData.thisSelect = undefined;
                        viewData.thisShowWindow && viewData.thisShowWindow.recover(), viewData.thisShowWindow = undefined;
                    }
                });

                viewData.thisSelect && sgworld.Core.zoomToFeature(viewData.thisSelect, map);
            });

            selected.on('remove', function (evt) {
                viewData.resultTooltip.show(false);
                let pickData = evt.element.values_;
                if (!pickData || !pickData.id || (pickData.id && pickData.id.indexOf('qiye') === -1)) {

                } else {
                    viewData.thisShowWindow && viewData.thisShowWindow.recover(), viewData.thisShowWindow = undefined;
                }
            });

        } else {
            viewData.selectHandler.setActive(true);
        }
        show.isPick = true;
    } else {
        show.isPick = false;
        map.un('pointermove', mouseMoveEven);
        viewData.selectHandler.setActive(false);
        viewData.tooltip && viewData.tooltip.show(false);
        document.body.style.cursor = "auto";
    }
}

//鼠标移动事件
function mouseMoveEven(evt) {
    let data = map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
        return {
            feature: feature,
            layer: layer
        };
    });

    if (data && data.feature) {
        if (data.feature.values_) {
            let value, _properties;
            viewData.thisPick = data.feature;
            if (viewData.thisSelect && viewData.thisSelect == viewData.thisPick) {
                document.body.style.cursor = "pointer";
                viewData.tooltip.show(false);
                return;
            }
            _properties = data.feature.values_;
            if (_properties.wzxq) {
                value = '<div style="font-family:微软雅黑;font-weight: bolder;font-size: 20px;padding:8px;">' + _properties.TE_DESC + '</div>' +
                    '<div style="font-family:黑体;font-weight: bolder;font-size: 15px;padding:2px 8px;">所选应急物资库存场所:</div><br>' +
                    '<div style="font-family:微软雅黑;font-weight: bolder;font-size: 18px;padding:6px;">' + _properties.wzxq + '</div>';
            } else {
                _properties.TE_DESC && (value = _properties.TE_DESC);
                _properties.name && (value = _properties.name);
            }
            if (value) {
                viewData.tooltip.showAt(value, evt.coordinate);
                document.body.style.cursor = "pointer";
            } else {
                viewData.thisPick = undefined;
                viewData.tooltip.show(false);
                document.body.style.cursor = "auto";
            }
        } else {
            viewData.thisPick = undefined;
            viewData.tooltip.show(false);
            document.body.style.cursor = "auto";
        }
    } else {
        viewData.thisPick = undefined;
        viewData.tooltip.show(false);
        document.body.style.cursor = "auto";
    }
}

//定位
$("#position").click(() => {
    sgworld.View.setCenter(ol.proj.fromLonLat([117.57844128806192, 38.35974915512376]));
    sgworld.View.setZoom(13.5);
});

//企业分级
$("#qiyefenji").click(() => {
    if (viewData.qiyem) {
        viewData.qiyem.isFourColor = !viewData.qiyem.isFourColor;
        if (viewData.qiyem.isFourColor) {
            viewData.qiyem.setStyle(function (feature) {
                let data = feature.values_;
                return new ol.style.Style({
                    fill: new ol.style.Fill({
                        color: data.color + '50'
                    }),
                    stroke: new ol.style.Stroke({
                        color: 'rgba(255, 255, 255, 1)',
                        width: 2
                    })
                });
            })
        } else {
            viewData.qiyem.setStyle(new ol.style.Style({
                fill: new ol.style.Fill({
                    color: 'rgba(255, 255, 255, 0.2)'
                }),
                stroke: new ol.style.Stroke({
                    color: 'rgba(255, 255, 255, 1)',
                    width: 2
                })
            }))
        }
    }
});

//聚合
$("#wzjh").click(() => {
    !show.isPick && _pickQiYeData();
    show.wzjh = !show.wzjh;
    show.wzxq = !show.wzxq;
    if (show.wzjh && !viewData.wzjh && show.wzxq && !viewData.wzxq) {
        viewData.wzjh = sgworld.Creator.AddGeoJSONFile('物资聚合', '../geojson/yingjiwuzi.geojson', {
            projection: 'EPSG:4326',
            minZoom: 13,
            maxZoom: 15,
            style: function (feature) {
                let data = feature.values_;
                return new ol.style.Style({
                    text: new ol.style.Text({
                        font: '20px 微软雅黑',
                        scale: 1,
                        text: data.TE_TEXT,
                        backgroundFill: new ol.style.Fill({
                            color: 'rgba(255, 255, 255, 0.8)'
                        }),
                        backgroundStroke: new ol.style.Stroke({
                            color: 'rgba(255, 255, 255, 1)',
                            width: 2
                        })
                    })
                });
            }
        });

        viewData.wzxq = sgworld.Creator.AddGeoJSONFile('物资详情', '../geojson/wuzixiangqing.geojson', {
            projection: 'EPSG:4326',
            image: '../static/IMG/warring.png',
            imageScale: 0.5,
            minZoom: 15,
            maxZoom: 25
        });
        return;
    }
    viewData.wzjh.setVisible(show.wzjh);
    viewData.wzxq.setVisible(show.wzxq);
});

//缓冲区
$("#hcq").click(() => {
    if (viewData.buff) {
        viewData.buff.clearBuff(), showBufferData(false);
        viewData.buff.start();
    } else {
        viewData.buff = sgworld.Analysis.DrawPointBuffer(180, function (position) {
            let degrees = ol.proj.toLonLat(position);
            showBufferData(true, degrees);
        });
    }
});

//缓冲区清除
$("#hcq_clear").click(() => {
    viewData.buff && viewData.buff.clearBuff(), showBufferData(false);
});

function showBufferData(isShow, degrees) {
    show.buff = isShow;
    if (isShow && viewData.wzxq) {
        viewData.wzxq.BufferData = [];
        !viewData.wzxq.source && (viewData.wzxq.source = viewData.wzxq.getSource().getFeatures());

        let distance;
        viewData.wzxq.source.forEach(item => {
            let Coordinates = item.getGeometry().flatCoordinates;
            Coordinates = [Coordinates[0], Coordinates[1]];
            let position = ol.proj.toLonLat(Coordinates);
            distance = ol.sphere.getDistance(position, degrees, 6371008.8);
            if (distance < 150) {
                if (item.divObj) {
                    item.divObj.show(true);
                } else {
                    item.divObj = sgworld.Creator.addDivPoint(Coordinates, { name: item.values_.TE_DESC });
                }
                viewData.wzxq.BufferData.push(item.divObj);
            }
        });
    } else {
        if (viewData.wzxq && viewData.wzxq.BufferData) {
            viewData.wzxq.BufferData.forEach(item => {
                item.show(false);
            });
        }
    }
    if (!viewData.addBuffEven) {
        viewData.addBuffEven = true;
        sgworld.View.on('change:resolution', checkZoom);//checkZoom为调用的函数

        function checkZoom() {
            let zoom = sgworld.View.getZoom();
            if (viewData.wzxq && viewData.wzxq.BufferData && show.buff && zoom < 15) {
                viewData.wzxq.BufferData.forEach(item => {
                    item.show(false);
                });
                viewData.buff.vector.setVisible(false)
            }
            if (viewData.wzxq && viewData.wzxq.BufferData && show.buff && zoom >= 15) {
                viewData.wzxq.BufferData.forEach(item => {
                    item.show(true);
                });
                viewData.buff.vector.setVisible(true)
            }
        }
    }
}

//重大危险源
function showDangerData(isShow) {
    show.danger = isShow;
    if (show.danger && !viewData.danger) {
        viewData.danger = sgworld.Creator.AddGeoJSONFile('重大危险源', '../geojson/zhongdaweixianyuan.geojson', {
            projection: 'EPSG:4326',
            minZoom: 12,
            style: function (feature) {
                let data = feature.values_;
                return new ol.style.Style({
                    fill: new ol.style.Fill({
                        color: 'rgba(255, 255, 255, 0.5)'
                    }),
                    stroke: new ol.style.Stroke({
                        color: data.color,
                        width: 2
                    })
                });
            }
        });
        return;
    }
    viewData.danger.setVisible(show.danger);
}

//重点工艺
function showCraftsData(isShow) {
    show.crafts = isShow;
    if (show.crafts && !viewData.crafts) {
        viewData.crafts = sgworld.Creator.AddGeoJSONFile('重点工艺', '../geojson/zhongdiangongyi.geojson', {
            projection: 'EPSG:4326',
            image: '../static/IMG/point1.png',
            imageScale: 0.5,
            minZoom: 12
        });
        return;
    }
    viewData.crafts.setVisible(show.crafts);
}

//重点危化品
function showDangerGoodsData(isShow) {
    show.DangerGoods = isShow;
    if (show.DangerGoods && !viewData.DangerGoods) {
        viewData.DangerGoods = sgworld.Creator.AddGeoJSONFile('重点危化品', '../geojson/zhongidanweihuapin.geojson', {
            projection: 'EPSG:4326',
            image: '../static/IMG/point.png',
            imageScale: 0.5,
            minZoom: 12
        });
        return;
    }
    viewData.DangerGoods.setVisible(show.DangerGoods);
}

//风险区域
function showDangerAreaData(isShow) {
    show.DangerArea = isShow;
    if (show.DangerArea && !viewData.DangerArea) {
        viewData.DangerArea = sgworld.Creator.AddGeoJSONFile('风险区域', '../geojson/fengxiansisetu.geojson', {
            projection: 'EPSG:4326',
            minZoom: 12,
            style: function (feature) {
                let data = feature.values_;
                return new ol.style.Style({
                    fill: new ol.style.Fill({
                        color: data.color + '50'
                    }),
                    stroke: new ol.style.Stroke({
                        color: 'rgba(255, 255, 255, 1)',
                        width: 2
                    })
                });
            }
        });
        return;
    }
    viewData.DangerArea.setVisible(show.DangerArea);
}

//图标数据
function showChartData(data, value) {
    let Coordinates = data.getGeometry().flatCoordinates;
    viewData.chartPosition = [Coordinates[0], Coordinates[1]];

    if (!viewData.addChartData) {
        viewData.addChartData = true;
        viewData.chartTooltip = sgworld.Core.createResultTooltip(map, false);
        viewData.chartTooltip.showAt('<div id="ChartDataTitle" style="white-space: nowrap;font-family:黑体;font-weight: bolder;font-size: 14px;color: white;padding: 4px 8px;border-bottom: 1px solid;"></div><iframe src="HTML/echart.html" style="height: 268px;width: 500px;border: unset;"></iframe>', viewData.chartPosition);

        viewData.ChartDataTitle = document.getElementById('ChartDataTitle');
    }
    viewData.ChartDataTitle.innerHTML = value;
    viewData.chartTooltip.show(true);
    viewData.chartTooltip.setPosition(viewData.chartPosition);
}

//关闭
function closeChartData() {
    if (viewData.chartTooltip) {
        viewData.chartTooltip.show(false);
    }
}

//文本点
$('#btn1').click(function () {
    if (!viewData.drawLabel) {
        viewData.drawLabel = sgworld.Creator.CreatePoint({
            text: '新建文本',
            font: '20px 宋体'
        });
    } else {
        viewData.drawLabel.pause();
    }
    viewData.drawImg && viewData.drawImg.draw.getActive() && viewData.drawImg.pause();
    viewData.drawLine && viewData.drawLine.draw.getActive() && viewData.drawLine.pause();
    viewData.drawPolygon && viewData.drawPolygon.draw.getActive() && viewData.drawPolygon.pause();
})

//图片点
$('#btn2').click(function () {
    if (!viewData.drawImg) {
        viewData.drawImg = sgworld.Creator.CreatePoint({
            image: 'http://36.7.136.132:1988/sdkdemo/src/static/image/mark.png',
            anchor: [0.5, 1]
        });
    } else {
        viewData.drawImg.pause();
    }
    viewData.drawLabel && viewData.drawLabel.draw.getActive() && viewData.drawLabel.pause();
    viewData.drawLine && viewData.drawLine.draw.getActive() && viewData.drawLine.pause();
    viewData.drawPolygon && viewData.drawPolygon.draw.getActive() && viewData.drawPolygon.pause();
})

//线
$('#btn3').click(function () {
    if (!viewData.drawLine) {
        viewData.drawLine = sgworld.Creator.CreatePolyline();
    } else {
        viewData.drawLine.pause();
    }
    viewData.drawLabel && viewData.drawLabel.draw.getActive() && viewData.drawLabel.pause();
    viewData.drawImg && viewData.drawImg.draw.getActive() && viewData.drawImg.pause();
    viewData.drawPolygon && viewData.drawPolygon.draw.getActive() && viewData.drawPolygon.pause();
})

//面
$('#btn4').click(function () {
    if (!viewData.drawPolygon) {
        viewData.drawPolygon = sgworld.Creator.CreatePolygon();
    } else {
        viewData.drawPolygon.pause();
    }
    viewData.drawLabel && viewData.drawLabel.draw.getActive() && viewData.drawLabel.pause();
    viewData.drawImg && viewData.drawImg.draw.getActive() && viewData.drawImg.pause();
    viewData.drawLine && viewData.drawLine.draw.getActive() && viewData.drawLine.pause();
})